package speech.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.StringTokenizer;

import edu.stanford.nlp.tagger.maxent.MaxentTagger;

import speech.tag.HmmSpeechActTagger;
import speech.tag.PosTagger;
import speech.tag.SpeechActTagger;

public class Main {
	public static void main(String[] args) {
		Parser p = new Parser();
//		ArrayList<String> dictionary = p.constructDictionaryFromPOSFiles("pos/");
//		System.out.println(dictionary);
		
//		p.encodePOSFiles(dictionary, "pos/");
		
/*		p.encodePOSFile(dictionary, "pos/Statement.txt", "train/Statement.seq");
		p.encodePOSFile(dictionary, "pos/test.txt", "test/test.seq");
		
		p.encodePOSFile(dictionary, "pos/POS_OpenQuestion.txt", "train/OpenQuestion.seq");
		p.encodePOSFile(dictionary, "pos/POS_testOpenQuestion.txt", "test/testOpenQuestion.seq");*/
		
/*		HmmSpeechActTagger tagger = new HmmSpeechActTagger("config.txt", dictionary.size());
		tagger.trainModels();
		System.out.println("Statement test:");
		tagger.testFile("test/test.seq");
		System.out.println("OpenQuestion test:");
		tagger.testFile("test/testOpenQuestion.seq");
		System.out.println("AllMighty test:");
		tagger.testFile("test/allMightyTest.seq");*/
		
		try {
			p.processRawTagTextFiles("test_training/all/raw/");
//			p.processRawTagTextFiles("raw/all/");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		String dataset = "all";
		String test_header = "Testing integration for " + dataset + " dataset.\n\n";
		
		Hashtable<String,Double> thresholds = new Hashtable<String,Double>();
		thresholds.put("a", 0.0);
		thresholds.put("aa", 0.3);
		thresholds.put("co", 0.0);
		thresholds.put("d", 0.0);
		thresholds.put("e", 0.0);
		thresholds.put("qo", 0.2);
		thresholds.put("qy", 0.1);
		thresholds.put("r", 0.0);
		thresholds.put("s", 0.1);
		thresholds.put("ss", 0.1);
		thresholds.put("t", 0.5);
		thresholds.put("sq", 0.5);
		
		ArrayList<String> tags = new ArrayList<String>();
		tags.add("a");
		tags.add("aa");
		tags.add("co");
		tags.add("d");
		tags.add("e");
		tags.add("qo");
		tags.add("qy");
		tags.add("r");
		tags.add("s");
		tags.add("ss");
		tags.add("t");
		
		Hashtable<String,Double> best = new Hashtable<String,Double>();
		Hashtable<String,Integer> max = new Hashtable<String,Integer>();
		
		for (String t : tags) {
			max.put(t, -1);
		}
		
/*		for (int i = 0; i < 10; ++ i) {
			double modifier = (double) i / 10;
			
			for (String t : tags) {
				thresholds.put(t, modifier);
			}
			
			SpeechActTagger system = new SpeechActTagger(4, thresholds);
			system.train();
			
			Hashtable<String,ArrayList<Integer>> summary = system.testHMM(test_header);
			
			for (String t : tags) {
//				System.out.println(t+ " " + summary.get(t) + " " + max.get(t));
				if (summary.get(t) != null && summary.get(t).get(0) > max.get(t)) {
					max.put(t, summary.get(t).get(0));
					best.put(t, modifier);
				}
			}
			
			System.out.println("PROGRESS >>>>>>>>>>>>>>>>>>>>>>> " + (i + 1) * 10 + "%\n");
		}
		
		System.out.println(best);
		System.out.println(max);*/
		
		int bestIntegrationThreshold = 1;
		
/*		try {
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("logs/" + System.currentTimeMillis() + "general_results.txt")));
			
			int maxAvg = -1;
			for (int i = 0; i < 10; ++ i) {
				pw.println("Integration Threshold: " + i);
				SpeechActTagger system = new SpeechActTagger(i, best);
				system.train();
//				system.testHMM(test_header);
//				system.testNBC(test_header);
				double result = system.test(test_header);
				pw.println("Avg match rate: " + result + "\n");
				if (result > maxAvg) {
					bestIntegrationThreshold = i;
					maxAvg = (int) result;
				}
				
				System.out.println("PROGRESS >>>>>>>>>>>>>>>>>>>>>>> " + (i + 1) * 10 + "%");
			}
		
			System.out.println(bestIntegrationThreshold);
			System.out.println(maxAvg);
	
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		
		SpeechActTagger system = new SpeechActTagger(bestIntegrationThreshold, thresholds);
		system.train();
		
		System.out.println("\n\n==============");
		System.out.println("SYSTEM TESTING");
		System.out.println("==============");
		
		system.testHMM(test_header);
		system.testNBC(test_header);
		system.test(test_header);
		
//		p.parseDialogue("dialogue.txt");
	}
}
